package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.util.ArrayMap;

import com.jess.arms.base.App;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.NoSortOutRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.api.AppConstant;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.NoSortOutBean;

import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/15/2018 13:57
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@FragmentScope
public class NoSortOutRecordPresenter extends BasePresenter<NoSortOutRecordContract.Model, NoSortOutRecordContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
     private ConfigBean configBean;
    @Inject
    public NoSortOutRecordPresenter(NoSortOutRecordContract.Model model, NoSortOutRecordContract.View rootView) {
        super(model, rootView);
        configBean=DataHelper.getDeviceData(mRootView.getMyActivity(),AppConstants.EXTRA_CONFIG_DATA);
    }

    /**
     * 查询未整理的记录
     * @param startTime
     * @param endTime
     * @param pageIndex
     */
    public void getUndigestedXcjl(String startTime,String endTime,int pageIndex)
    {
//        String url=DataUtil.getJNUrl(configBean)+"/hzxh";
        String url=DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getUndigestedXcjl";
        Map<String,Object> maps=new ArrayMap<>();
        maps.put("beginDate",startTime);
        maps.put("current",pageIndex);
        maps.put("endDate",endTime);
        maps.put("gpsid",configBean.getGpsId());//configBean.getGpsId()
        maps.put("size",Api.PAGE_SIZE_1);
        mModel.getUndigestedXcjl(url,maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<NoSortOutBean>(mErrorHandler) {
                    @Override
                    public void onNext(NoSortOutBean noSortOutBean) {
                        if (noSortOutBean==null||noSortOutBean.getCode()!=0||noSortOutBean.getData()==null)
                        {
                            mRootView.showMessage("查询数据异常");
                            return;
                        }
                        mRootView.getUndigestedXcjlSuccess(noSortOutBean.getData().getRecords(),noSortOutBean.getData().getPages());
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
